package peace.design.command
{
	import flash.events.Event;
	
/**
 * команда для параллельного выполнения группы команд
 */
public class BoxCommand extends Command
{
	
	public var list:Array = [];
	
	/**
	 * счетчик выполненных команд 
	 */	
	private var counter:int;
		
	public function BoxCommand(...commands)
	{
		if (commands.length > 0)
		{
			if (commands[0] is Array)
			{
				this.list = commands[0] as Array;				
			}
			else
			{
				for each (var c:Command in commands)
				{
					list.push(c);
				}
			}
		}
	}
	
	override public function execute ():void
	{
		super.execute();
		
		counter = 0;
		
		for each (var c:Command in list)
		{
			c.addEventListener(Event.COMPLETE, onComplete, false,0,true);
			c.execute();
		}
	}
	
	private function onComplete (e:Event):void
	{
		counter++;
		
		Command (e.target).removeEventListener(Event.COMPLETE, onComplete);
		
		if (counter == list.length)
		{
			notifyComplete();
		}
	}

}

}